package io.admin.common.utils;

import io.admin.modules.sys.entity.BaiduMapImgEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.Units;
import org.apache.poi.wp.usermodel.Paragraph;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.IOException;
import java.io.*;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class BaiduMapImageExporter {

        private static final String BAIDU_MAP_URL_TEMPLATE = "https://api.map.baidu.com/staticimage/v2?mcode=666666&center=%s&zoom=%d&width=%d&height=%d&ak=%s";
        private static final String TD_MAP_URL_TEMPLATE = "http://api.tianditu.gov.cn/staticimage?center=116.40,39.93&width=400&height=300&zoom=10&tk=您的密钥";


    private static final String BAIDU_MAP_API_KEY = "JxrDPsHM3UmoV2CkCqCYXPXixLHZBTkd"; // 替换为你的百度地图API Key
    private final  static String ADDRESS_FILE =ExcelUtils.ADDRESS_FILE;
        public static void exportBaiduMapImage(String lat, String lon,
                                               int zoom, int width,
                                               int height, String filePath, List<BaiduMapImgEntity> list) throws
        IOException {
            String baiduMapUrl = String.format(BAIDU_MAP_URL_TEMPLATE, lat + "," + lon, zoom, width, height, BAIDU_MAP_API_KEY);
            //开始获取标点
            String markers = "&markers=";
            Integer index=1;
            String markerStyles = "&markerStyles=";
            String paths ="&paths=";
            String labels ="&labels=";
            String labelStyles ="&labelStyles=";
            String pathStyles ="&pathStyles=0xff0000,1,1";
            if(list.size()>1){
                for (BaiduMapImgEntity mapImg:list){
                    markers+=mapImg.getLgt()+","+mapImg.getDms();
                   /* mapImg.setIcon(mapImg.getIcon().replaceAll("downloadLocal","downloadLocalByHalf"));*/
                    markerStyles+="-1,"+mapImg.getIcon()+",30,30";
                    labelStyles+=mapImg.getName()+",0,8,ffffff,1890ff,1";
                    paths+=mapImg.getLgt()+","+mapImg.getDms();
                    labels+=mapImg.getLgt()+","+mapImg.getDms();
                    if(!index.equals(list.size())){
                        markers+="|";
                        labels+="|";
                        labelStyles+="|";
                        markerStyles+="|";
                        paths+=";";
                    }
                    index++;
                }
                baiduMapUrl =baiduMapUrl+markers+markerStyles+paths+pathStyles+labels+labelStyles;
            }else{
                BaiduMapImgEntity mapImg = list.get(0);
                markers+=mapImg.getLgt()+","+mapImg.getDms();
               /* mapImg.setIcon(mapImg.getIcon().replaceAll("downloadLocal","downloadLocalByHalf"));*/
                markerStyles+="-1,"+mapImg.getIcon()+",30,30";
                baiduMapUrl =baiduMapUrl+markers+markerStyles;
            }
            extracted(filePath, baiduMapUrl);
        }

    public static void extracted(String filePath, String baiduMapUrl) throws IOException {
        URL url = new URL(baiduMapUrl);
        System.out.println(baiduMapUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoInput(true);
        connection.connect();
        try (InputStream inputStream = connection.getInputStream();
             FileOutputStream fileOutputStream = new FileOutputStream(ADDRESS_FILE+ filePath)) {
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, bytesRead);
            }
        }catch (Exception e){
            log.error(e.getMessage());
        }finally {
            connection.disconnect();
        }
    }


    private static void extracted() throws IOException {
        // 创建一个新的Word文档
        XWPFDocument doc = new XWPFDocument();
        XWPFParagraph paragraph = doc.createParagraph();
        paragraph.setAlignment(ParagraphAlignment.CENTER);
        paragraph.createRun().setText("标题");
        // 创建一个表格
        Integer rowNum =9;
        Integer cellNum =2;
        XWPFTable table = doc.createTable(rowNum, cellNum);
        XWPFTableRow row = table.getRow(0); // 获取行
        // 获取表格中的单元格
        XWPFRun run = row.getCell(1).getParagraphs().get(0).createRun();
        // 图片文件路径
        String imgFile = ADDRESS_FILE+"mapList.jpg";
        FileInputStream fis = new FileInputStream(imgFile);
        try {
            run.addPicture(fis, XWPFDocument.PICTURE_TYPE_JPEG, imgFile, Units.toEMU(400), Units.toEMU(300));
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        }
        table.getRow(1).getCell(0).setText("嗨 ");
        table.getRow(1).getCell(1).setText("wode");

        // 将文档保存到磁盘
        try (FileOutputStream out = new FileOutputStream(ADDRESS_FILE+DateUtil.currentTimestampToString()+"output.docx")) {
            doc.write(out);
        }catch (Exception e){

        }
        // 关闭文档
        doc.close();
    }

    public static void main(String[] args) throws Exception {
        String str ="https://api.map.baidu.com/staticimage/v2" +
                "?mcode=666666&center=113.17,23.8&zoom=15&width=1000&height=900&ak=JxrDPsHM3UmoV2CkCqCYXPXixLHZBTkd" +
                "&markers=113.17,23.8|113.171,23.81|113.172,23.82|" +
                "&markerStyles=-1,http://www.lezhiii.com/admin/sys/oss/downloadLocal?name=223d87aa02354b2cb4eeac1b3005290a.png,30,30|" +
                "-1,http://www.lezhiii.com/admin/sys/oss/downloadLocal?name=223d87aa02354b2cb4eeac1b3005290a.png,30,30|" +
                "-1,http://www.lezhiii.com/admin/sys/oss/downloadLocal?name=7c74434d2380485c9f8ff3fb70ecb019.png,30,30|" +
                "&paths=113.17,23.8;113.171,23.81;113.172,23.82;&pathStyles=0xff0000,1,1"+
                "&labels=113.17,23.8|113.171,23.81|113.172,23.82"+
                "&labelStyles=测试标记1,1,32,0x990099,0xff00,1" +
                "|测试标记2,1,14,1890ff,ffffff,1" +
                "|测试标记3,1,14,ffffff,1890ff,1" ;
    }

}
